﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SlamCms.SharePoint.Core
{
	public class SharePointSite : SharePointAction<SharePointSite>
	{
		private SharePointSiteCollection _siteCollection;

		public SharePointSite() :
			base()
		{
			_siteCollection = new SharePointSiteCollection(SharePointItem);
		}

		public SharePointSite(SharePointItem baseItem)
		{
			SharePointItem = baseItem;
			_siteCollection = new SharePointSiteCollection(SharePointItem);
		}

		public SharePointList List(string listName)
		{
			SharePointItem.ListName = listName;
			return new SharePointList(SharePointItem);
		}

		public SharePointList List(Guid listId)
		{
			SharePointItem.ListId = listId;
			return new SharePointList(SharePointItem);
		}

		public void List(string listName, Action<SPList> action)
		{
			_siteCollection.Site(s =>
			{
				action.Invoke(s.Lists.TryGetList(listName));
			});
		}

		public void List(string listName, Action<SPList, SPWeb> action)
		{	
			_siteCollection.Site(s =>
			{
				action.Invoke(s.Lists.TryGetList(listName), s);
			});
		}

		public void List(string listName, Action<SPList, SPWeb, SPSite> action)
		{
			_siteCollection.Site((s, sc) =>
			{
				action.Invoke(s.Lists.TryGetList(listName), s, sc);
			});
		}

		public void List(Guid listId, Action<SPList> action)
		{
			_siteCollection.Site(s =>
			{
				action.Invoke(s.Lists.GetList(listId, true));
			});
		}

		public void List(Guid listId, Action<SPList, SPWeb> action)
		{
			_siteCollection.Site(s =>
			{
				action.Invoke(s.Lists.GetList(listId, true), s);
			});
		}

		public void List(Guid listId, Action<SPList, SPWeb, SPSite> action)
		{
			_siteCollection.Site((s, sc) =>
			{
				action.Invoke(s.Lists.GetList(listId, true), s, sc);
			});
		}
	}
}
